<?php

namespace app\storeapi\controller\apps\set;
use app\storeapi\controller\Controller;
use app\common\model\SettingLog as SettingLogSettingModel;
use app\factory\MqFactory;

/**
 * 营销设置-分红设置
 * Class Basic
 * @package app\store\controller
 */
class Setting extends Controller
{

    /**
     * 分红比例设置详情
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        $nowTime = time();
        $wxapp_id = $param['wxapp_id'];

        $values = db('wxapp_setting')->where(['wxapp_id'=>$wxapp_id])->find();

        if($values){
            $values['start_time'] = date('Y-m-d H:i:s', $values['start_time']);
//            if($values['end_time']<$nowTime){
//                $values['start_time'] = date('Y-m-d H:i:s', $nowTime);
//            }
        }else{
            $values = [];
        }
        return $this->renderSuccess($values);

    }


    /**
     * 分红比例设置
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function set()
    {
        $param = $this->postData();

        $nowTime = time();
        $wxapp_id = $param['wxapp_id'];
        //时间判断
        $getTime = strtotime($param['start_time']);
        $endTime =  $getTime+$param['term']*24*60*60;
        $is_exsit = db('wxapp_setting')->where(['wxapp_id'=>$wxapp_id])->find();

        //判断分红周期
        $term = $param['term'];
        if($term<1 || $term>180){
            return $this->renderError('分红周期为1-180');
        }

        //判断分红上限
        $upline = $param['upline'];

        if($upline<=0){
            return $this->renderError('分红上限不能小于0');
        }

        if($upline>100){
            return $this->renderError('分红上限不能超过100');
        }

        //分红拨比
        $rewardpercent = $param['rewardpercent'];

        if($rewardpercent<3 || $rewardpercent>50){
            return $this->renderError('分红拨比范围为3~50');
        }
        if(!isset($param['real_reward'])){
            $real_reward = 0;
        }else{
            $real_reward = $param['real_reward'];
        }

        $updateDate = [
            'wxapp_id' => $wxapp_id,
            'money' => $param['money'],//分红条件
            'start_time' => $getTime,//开始时间
            'end_time' => $endTime,//结束时间
            'term' =>$term,//分红周期
            'upline' => $upline,//分红上限
            'rewardpercent' => $param['rewardpercent'],//分红拨比
            'moneypercent' => $param['moneypercent'],//零钱比例
            'moneypercentstatus' => $param['moneypercentstatus'],//零钱比例开关 1 开启 2关闭
            'status' => $param['status'],//分红开关 1 开启 2关闭
            'real_reward' => $real_reward,//实物是否参与分红 0 否 1是
        ];

        $settingLog = new SettingLogSettingModel();

        if($is_exsit){
            //以前的开始时间
            $oldStartTime = $is_exsit['start_time'];
            //以前的结束时间
            $oldEndTime = $is_exsit['end_time'];

            if($getTime!=$oldStartTime){
                //出局处理
                $rmq = MqFactory::instance();

                $data = ['wxapp_id'=>$wxapp_id,'periods_num'=>$is_exsit['periods_num']];

                $rmq->send($data,'divideReceives','divideReceivesLog','divideReceivesLog');

                $updateDate['periods_num'] = $is_exsit['periods_num']+1;
            }else{
                $updateDate['periods_num'] = $is_exsit['periods_num'];
            }

            //未开始
            if($oldStartTime>$nowTime ){

                $flag = db('wxapp_setting')->where(['wxapp_id'=>$wxapp_id])->update($updateDate);

            }elseif($oldStartTime<=$nowTime && $oldEndTime>=$nowTime){

                $flag = db('wxapp_setting')->where(['wxapp_id'=>$wxapp_id])->update($updateDate);

            }elseif($oldEndTime<$nowTime){

//                if($getTime<$nowTime){
//                    return $this->renderError('时间不能小于当前时间');
//                }

                $flag = db('wxapp_setting')->where(['wxapp_id'=>$wxapp_id])->update($updateDate);

            }

            //写入分红配置日志 start
            //设备类型
            $deviceType = getDeviceType();
            $logData =[
                'type'=>1,//1：分红设置  2：会员卡分红设置
                'create_time'=>time(),
                'client'=>"分红设置",
                'deviceType'=>$deviceType,
                'wxapp_id'=>$wxapp_id
            ];
            $settingLog->addData($logData,$updateDate);
        }else{
//            if($getTime<$nowTime){
//                return $this->renderError('时间不能小于当前时间');
//            }

            $insertData = [

                'wxapp_id' => $wxapp_id,
                'money' => $param['money'],//分红条件
                'start_time' => $getTime,//开始时间
                'end_time' => $endTime,//结束时间
                'term' => $param['term'],//分红周期
                'upline' => $param['upline'],//分红上限
                'rewardpercent' => $param['rewardpercent'],//分红拨比
                'moneypercent' => $param['moneypercent'],//零钱比例
                'moneypercentstatus' => $param['moneypercentstatus'],//零钱比例开关 1 开启 2关闭
                'status' => $param['status'],//分红开关 1 开启 2关闭
                'periods_num' => 1000,
                'real_reward' => $real_reward,//实物是否参与分红 0 否 1是

            ];

            //写入分红配置日志 start
            //设备类型
            $deviceType = getDeviceType();
            $logData = [
                'type'=>1,//1：分红设置  2：会员卡分红设置
                'create_time'=>time(),
                'client'=>"分红设置",
                'deviceType'=>$deviceType,
                'wxapp_id'=>$wxapp_id
            ];
            $settingLog->addData($logData,$insertData);


            $flag = db('wxapp_setting')->insert($insertData);
        }

        if($flag){
            return $this->renderSuccess('操作成功');
        }else{
            return $this->renderError('数据未修改');
        }


    }


    //密码验证
    public function checkPass(){

        $param = $this->postData();

        $wxapp_id = $param['wxapp_id'];
        $password = $param['password'];
        if(!isset($password) || empty($password)){
            return $this->renderError('请输入密码');
        }

        $newpassword = yoshop_hash($password);

        $getPass = db('wxapp')->where(['wxapp_id'=>$wxapp_id])->value('password');
        if(!$getPass){
            db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['password'=>$newpassword]);
        }else{
            if($newpassword!=$getPass){
                return $this->renderError('密码错误');
            }
        }
        return $this->renderSuccess([],'验证成功');

    }



    //忘记密码
    public function forgetPass(){

        $param = $this->postData();

        $wxapp_id = $param['wxapp_id'];
        $password = $param['password'];
        if(!isset($password) || empty($password)){
            return $this->renderError('请输入密码');
        }

        $newpassword = yoshop_hash($password);


        db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['password'=>$newpassword]);

        return $this->renderSuccess([],'重置密码成功');

    }





}